package coms.dataaccess.hibernate;


import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import coms.dataaccess.DAOFactory;
import coms.dataaccess.FileItemDAO;
import coms.dataaccess.ReviewDao;
//import coms.datatransfer.AssignedReviewDTO;
import coms.datatransfer.FileItemDTO;
import coms.datatransfer.ReviewDTO;

public class ReviewDaoImpl extends AbstractDAO implements ReviewDao {
	
	public ReviewDaoImpl(){
		super();
	}
		
	@Override
	public void createOrUpdate(ReviewDTO reviewDto){
		super.saveOrUpdate(reviewDto);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<ReviewDTO> displayAll(){
		return super.findAll(ReviewDTO.class);		
	}
		
	@SuppressWarnings("unchecked")
	@Override
	public List<ReviewDTO> findByFileId(Integer fileId){
		return super.findByCategory(ReviewDTO.class, "fileID", fileId.toString());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ReviewDTO> displayReviewedFile(Integer userID) {
		//List<ReviewDTO> temp = new ArrayList<ReviewDTO>();
		
		List<ReviewDTO> reviewedList = (List<ReviewDTO>)super.findByCategory(ReviewDTO.class, "reviewerID", userID.toString());
		/*List<Object[]> assignedReview = (List<Object[]>)super.manyToManyfindById(FileItemDTO.class,"listOfReviewers", "userID", userID.toString());
		System.out.println("assigned size"+ assignedReview.size());
		
		for (Object[] a : assignedReview){		
			
			FileItemDTO file = (FileItemDTO)a[0];
			for(ReviewDTO r : reviewedList){
				if(file.getFileID() == r.getFileID()){
					temp.add(r);
				}
			}
		}*/
		
		return reviewedList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> displayAssignedFile(Integer userID) {
		
		//System.out.println("inside dao"+ userID);
		//return (List<Object[]>)super.manyToManyfindById(FileItemDTO.class,"listOfReviewers", "userID", userID.toString());
		List<Object[]> temp = new ArrayList<Object[]>();		
		
		List<ReviewDTO> reviewedList = (List<ReviewDTO>)super.findByCategory(ReviewDTO.class, "reviewerID", userID.toString());
		List<Object[]> assignedReview = (List<Object[]>)super.manyToManyfindById(FileItemDTO.class,"listOfReviewers", "userID", userID.toString());
		System.out.println("assigned size"+ assignedReview.size());
		
		
		
		for (Object[] a : assignedReview){
			FileItemDTO file = (FileItemDTO)a[0];
			Boolean present=false;
			for(ReviewDTO r : reviewedList){
				if(file.getFileID() == r.getFileID()){
					present = true;
				}
			}
			
			if(present==false){
				temp.add(a);
			}
		}		
		return temp;
	}
	
	@SuppressWarnings("unchecked")
	public ReviewDTO displayReviewByFileAndUser(Integer fileID, Integer userID){
		ReviewDTO temp = new ReviewDTO();
		
		List<ReviewDTO> listByFiles = (List<ReviewDTO>)super.findByCategory(ReviewDTO.class, "fileID", fileID.toString());
		//List<ReviewDTO> listByFiles = (List<ReviewDTO>)super.find(ReviewDTO.class, fileID);
		//List<ReviewDTO> listByUsers = (List<ReviewDTO>)super.find(ReviewDTO.class, userID);
		for(ReviewDTO f : listByFiles){
			if(f.getReviewerID() == userID){
				temp = f;
			}
		}
		
		return temp;
	}

}
